<?php

namespace App\Module\Api\Controllers;

use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\LoginRequest;
use App\Module\Api\Validator\LoginValidator;
use App\System\Base\Repositories\BaseSmsCodeRepository;
use App\System\Cis\Repositories\CisUserRepository;

class LoginController extends Controller
{

     public function __construct()
     {
         $this->query = new LoginRequest();
         parent::__construct();
     }
    public function index()
    {
        (new LoginValidator())->check();
        $action = $this->query->action;
        $user = CisUserRepository::getInstance();

        if ($action==2){
//            手机号登录
//            验证手机号是否存在
            $hasMobile = $user->hasMobile($this->query->mobile);
            if (!$hasMobile){
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '手机号不存在！',
                ]);
            }

            $userInfo = $user->UserInfoByMobile($this->query->mobile,$this->query->password);

            if (!$userInfo){
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '密码错误！',
                ]);
            }

            //把用户信息放入到对应的s中
          $cache = \Cache::get(request()->s);
            $cache['userInfo']=$userInfo;
           \Cache::put(request()->s,$cache,config('app.cache_time'));
              return apiResponse(['isBindMobile'=>2,'d'=>'登录成功']);

        }
    }
}
